// 加密函数
export  function encryptAES(text, key) {
    const encodedText = new TextEncoder().encode(text);
    const encodedKey =  window.crypto.subtle.importKey(
      'raw',
      new TextEncoder().encode(key),
      'AES-CBC',
      false,
      ['encrypt']
    );
  
    const iv = window.crypto.getRandomValues(new Uint8Array(16));
    const encrypted =  window.crypto.subtle.encrypt(
      { name: 'AES-CBC', iv },
      encodedKey,
      encodedText
    );
  
    // 返回加密后的结果和 IV（Initialization Vector）
    return { encryptedData: encrypted, iv };
  }
  
  // 解密函数
  export   function decryptAES(encryptedData, key, iv) {
    const encodedKey =  window.crypto.subtle.importKey(
      'raw',
      new TextEncoder().encode(key),
      'AES-CBC',
      false,
      ['decrypt']
    );
  
    const decrypted =  window.crypto.subtle.decrypt(
      { name: 'AES-CBC', iv },
      encodedKey,
      encryptedData
    );
  
    return new TextDecoder().decode(decrypted);
  }